#include "ModifyTextCmd.h"

#include "../model/UMLObject.h"

#include <stdexcept>

ModifyTextCmd::ModifyTextCmd(UMLObject* target, const QString& text)
:	Command(),
	_executed(false),
	_target(target),
	_text(text) // copy-on-write is beautiful *sniff*
{
	if (!target)
		throw std::domain_error("Cannot modfiy a NULL UMLObject.");
}

const QString& ModifyTextCmd::text() const
{
	return _text;
}

void ModifyTextCmd::setText(const QString& text)
{
	_text = text;
}

void ModifyTextCmd::execute()
{
	if (!_executed)
		_oldtext = _target->text().fullText();

	_target->setText(_text); // constructs temporary BUMLSText

	_executed = true;
}

void ModifyTextCmd::unexecute()
{
	if (!_executed)
		return;

	_target->setText(_oldtext);

	_executed = false;
}

bool ModifyTextCmd::useful() const
{
	return (_executed) ?
		(_oldtext != _text) :
		(_target->text().fullText() != _text);
}

const char* ModifyTextCmd::name() const
{
	return "ModifyTextCmd";
}

